﻿Imports TBCC.UtilityMobile
Public Class DataUploadIntervalForm

    Private _pi As ProjectItem
    Private _netid As Byte = g_netID
    Private _isModify As Boolean = False

    Private Const _CollectIntervalOffset As UShort = 2001
    Private Const _RecordIntervalOffset As UShort = 2004

    Private Const _cInvalMin As Integer = 10
    Private Const _rInvalMin As Integer = 10

    Private Sub DataUploadIntervalForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CenterToParent()

        _pi = Me.MyParentForm.PI

        If Me.MyParentForm IsNot Nothing Then
            Me.MyParentForm.SetControlEnabled(False)
        End If
        Me.Timer1.Start()
        'Me.GroupBox1.Enabled = True

    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click

        If _pi.LocalRunStatus = LocalRunStatus.Start Then
            msg.ShowError("数据记录正在运行，请先停止数据记录。")
            Exit Sub
        End If

        Me.btnModify.Enabled = False
        If EnterModify() = False Then
            Me.btnModify.Enabled = True
            Exit Sub
        End If

        SetModifyState(True)
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim result As Boolean

        Me.Cursor = Cursors.WaitCursor
        result = Save()
        Me.Cursor = Cursors.Default
        If result Then
            SetModifyState(False)
            msg.Show("保存成功。")
        Else
            msg.ShowError("保存失败。")
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Timer1.Stop()
        If RefreshData() = True Then
            Me.btnModify.Enabled = True
            Me.nudCollectInterval.Minimum = _cInvalMin
            Me.nudRecordInterval.Minimum = _rInvalMin
        End If
        If Me.MyParentForm IsNot Nothing Then
            Me.MyParentForm.SetControlEnabled(True)
        End If
    End Sub

    Private Sub nudCollectInterval_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudCollectInterval.ValueChanged
        Me.nudRecordInterval.Minimum = Me.nudCollectInterval.Value
        Me.nudRecordInterval.Increment = Me.nudCollectInterval.Value
    End Sub

    '获取数据
    Private Function RefreshData() As Boolean
        Dim buf() As UShort

        '数据采集间隔
        buf = COMHelper.Instance.Send4UShort(_netid, FunctionCode.WordWritableRead, _CollectIntervalOffset, 1)
        If buf Is Nothing OrElse buf.Length <= 0 Then
            msg.ShowError(g_strReadError)
            Return False
        End If
        Me.nudCollectInterval.Value = buf(0)
        Me.nudCollectInterval.Tag = buf(0)

        '数据记录间隔
        buf = COMHelper.Instance.Send4UShort(_netid, FunctionCode.WordWritableRead, _RecordIntervalOffset, 1)
        If buf Is Nothing OrElse buf.Length <= 0 Then
            msg.ShowError(g_strReadError)
            Return False
        End If
        Me.nudRecordInterval.Value = buf(0)
        Me.nudRecordInterval.Tag = buf(0)

        Return True
    End Function

    '设置控件修改状态
    Private Sub SetModifyState(ByVal flag As Boolean)
        Me.btnModify.Enabled = Not flag
        Me.btnOK.Enabled = flag
        Me.GroupBox1.Enabled = flag
    End Sub

    '开始修改
    Private Function EnterModify() As Boolean
        If g_test Then
            _isModify = True
            Return True
        End If


        Dim result As Boolean
        result = ParameterModifyClass.EnterModify(_netid)

        If result = False Then
            Return False
        End If

        ''开始发送心跳包
        'Me.TimerHeart.Start()

        _isModify = True

        Return True
    End Function

    '取消修改
    Private Function ExitModify() As Boolean
        Dim result As Boolean
        result = ParameterModifyClass.ExitModify(_netid)

        ''停止发送心跳包
        'Me.TimerHeart.Stop()

        _isModify = False

        Return True
    End Function

    '保存参数
    Private Function Save() As Boolean
        Dim result As Boolean

        '数据采集间隔
        If Me.nudCollectInterval.Value <> Me.nudCollectInterval.Tag Then '已经修改
            result = Save(_CollectIntervalOffset, Me.nudCollectInterval.Value)
            If result = False Then
                GoTo FailedHandler
            End If
        End If

        '数据记录间隔
        If Me.nudRecordInterval.Value <> Me.nudRecordInterval.Tag Then '已经修改
            result = Save(_RecordIntervalOffset, Me.nudRecordInterval.Value)
            If result = False Then
                GoTo FailedHandler
            End If
        End If

        '设置修改使能变量
        If ParameterModifyClass.SetModifyConfirm(_netid) = False Then
            Return False
        End If

        'If ExitModify() = False Then
        '    Debug.Write("ExitModify错误")
        'End If

        Return True

FailedHandler:
        If ExitModify() = False Then
            Debug.Write("ExitModify错误")
        End If
        Return False

    End Function

    Private Function Save(ByVal offset As UShort, ByVal value As UShort) As Boolean
        offset = offset + 1000
        Return COMHelper.Instance.Write(_netid, FunctionCode.WordWrite, offset, value)
    End Function

End Class